package com.controller;
import com.pojo.Content;
import com.pojo.Search;
import com.pojo.User;
import com.service.ContentService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController //该Controller全部返回JSON格式
@RequestMapping("/content")
public class ContentController {
    @Autowired
    private ContentService contentService;
    @Autowired
    private UserService userService;


    //    查询用户的所有待办事项
    @RequestMapping(value = "/todo/{userId}",method = RequestMethod.POST)
    public List<Content> searchTodo(@PathVariable Integer userId){
        List<Content> todoList = contentService.todoList(userId);
        return todoList;
    }

    //    添加待办
    @RequestMapping(value = "/addTodo",method = RequestMethod.POST)
    public List<Content> insertContent(@RequestBody Content content){
//        在添加的时候把userId作为参数传过来
        contentService.insertContent(content);
        System.out.println(content.getUserId());
        Integer userId = content.getUserId();
        /*再次查询所有待办*/
        List<Content> todoList = contentService.todoList(userId);
        System.out.println("添加成功");
        return todoList;
    }

    //    删除待办
    @RequestMapping(value = "/deleteTodo", method = RequestMethod.POST)
    public List<Content> deleteContent(@RequestBody Content content){
//        删除时候把userId作为参数传过来
        Integer userId = content.getUserId();
        Integer id = content.getId();
        /*先删再查*/
        contentService.deleteContent(id,userId);
        List<Content> todoList = contentService.todoList(userId);
        System.out.println("删除成功");
        return todoList;
    }

    /*点击哪天就显示哪天的待办*/
    @RequestMapping(value = "/findTodayList", method = RequestMethod.POST)
    public List<Content> findTodayList(@RequestBody Content content){
        Integer userId = content.getUserId();
        String startDate = content.getStartDate();
        List<Content> todaylist = contentService.todaylist(startDate, userId);
        System.out.println("查询成功");
        return todaylist;
    }

    //    更新待办状态
    @RequestMapping(value = "/updateStatus",method = RequestMethod.PUT)
    public List<Content> updateStatus(@RequestBody Content content){
        Integer id = content.getId();
        Integer status = content.getStatus();
        contentService.workDone(id,status);
        Integer userId = content.getUserId();
        List<Content> contents = contentService.todoList(userId);
        return contents;

    }

    //  修改待办（内容）
    @RequestMapping(value = "/updateContent",method = RequestMethod.PUT)
    public void updateContent(@RequestBody Content content){
        String content1 = content.getContent();
        Integer id1 = content.getId();
        contentService.updateContent(content1,id1);
        System.out.println("内容修改成功");
    }

    //    修改待办（地点）
    @RequestMapping(value = "/updateAddress",method = RequestMethod.PUT)
    public void updateAddress(@RequestBody Content content){
        String address = content.getAddress();
        Integer id = content.getId();
        contentService.updateAddress(address,id);
        System.out.println("地点修改成功");
    }

    //    修改待办（类型）
    @RequestMapping(value = "/updateType",method = RequestMethod.PUT)
    public void updateType(@RequestBody Content content){
        String type = content.getType();
        Integer id = content.getId();
        contentService.updateType(type,id);
        System.out.println("类型修改成功");
    }

    //    修改待办（开始日期）
    @RequestMapping(value = "/updateStartDate",method = RequestMethod.PUT)
    public void updateStartDate(@RequestBody Content content){
        String startDate = content.getStartDate();
        Integer id = content.getId();
        contentService.updateStartDate(startDate,id);
        System.out.println("内容修改成功");
    }

    //    修改待办（结束日期）
    @RequestMapping(value = "/updateEndDate",method = RequestMethod.PUT)
    public void updateEndDate(@RequestBody Content content){
        String endDate = content.getEndDate();
        Integer id = content.getId();
        contentService.updateEndDate(endDate,id);
        System.out.println("内容修改成功");
    }

    //    修改待办（日期）
    @RequestMapping(value = "/updateDate",method = RequestMethod.PUT)
    public void updateDate(@RequestBody Content content){
        String startDate  = content.getStartDate();
        String endDate = content.getEndDate();
        Integer id = content.getId();
        contentService.updateDate(startDate,endDate,id);
        System.out.println("内容修改成功");
    }

    //    修改待办（参与者）
    @RequestMapping(value = "/updatePartner",method = RequestMethod.PUT)
    public void updatePartner(@RequestBody Content content){
        String partner = content.getPartner();
        Integer id = content.getId();
        contentService.updatePartner(partner,id);
        System.out.println("内容修改成功");
    }

    //    修改待办（所有）
    @RequestMapping(value = "/updateTodo",method = RequestMethod.PUT)
    public void updateTodo(@RequestBody Content content){
        contentService.updateTodo(content);
        System.out.println("内容修改成功");
    }


    //    随便按照什么查
    @RequestMapping(value = "/findByAnyWhat",method = RequestMethod.POST)
    public List<Content> findByAnyWhat(@RequestBody Content content){
        List<Content> todoList = contentService.findByAnyWhat(content);
        return todoList;
    }

    //    查询当日的所有待办
    @RequestMapping(value = "/findByDay",method = RequestMethod.POST)
    public List<Content> findTodoByDay(@RequestBody Content content){
        Integer userId = content.getUserId();
        List<Content> todoList = contentService.findTodoByDay(userId);
        return todoList;
    }

    /*按id查询*/
    @RequestMapping(value = "/findById/{id}",method = RequestMethod.GET)
    public Content findById(@PathVariable Integer id){
        Content content= contentService.findById(id);
        return content;
    }

    /*week系列查询*/
    /*按id查询*/
    @RequestMapping(value = "/findById2",method = RequestMethod.POST)
    public List<Content> findById2(@RequestBody Content content){

        List<Content> byId =  contentService.findById2(content.getId());

        return byId;
    }


    //    查询最近一周待办
    @RequestMapping(value = "/findByWeek",method = RequestMethod.POST)
    public List<Content> findTodoByWeek(@PathVariable Integer userId){
        List<Content> todoList = contentService.findTodoNearSeven(userId);
        return todoList;
    }

    //    查询本月待办
    @RequestMapping(value = "/findByMonth",method = RequestMethod.POST)
    public List<Content> findTodoByMonth(@PathVariable Integer userId){
        System.out.println(userId);
        List<Content> todoList = contentService.findTodoByMonth(userId);
        return todoList;
    }


    //    查询想要的月的数据
    @RequestMapping(value = "/findByWantMonth/{wantMonth}",method = RequestMethod.POST)
    public List<Content> findByWantMonth(@PathVariable String wantMonth,@PathVariable Integer userId){
        List<Content> todoList = contentService.findTodoByWantMonth(wantMonth,userId);
        return todoList;
    }

    //    计算待办完成率
    @RequestMapping(value = "/donePercent/{userId}",method = RequestMethod.POST)
    public Map<String, Double> donePercent(@PathVariable Integer userId){
        Map<String,Double> mapPercent = contentService.mapPercent(userId);
        return mapPercent;
    }

    // 按照给的关键字查
    @RequestMapping(value = "/findAny",method = RequestMethod.POST)
    public List<Content> findAny(@RequestBody Search search){
        String keywords = search.getKeywords();
        Integer userId = search.getUserId();
        List<Content> list = contentService.findAny(keywords,userId);
        return list;
    }

    //    查询指定月的待办数据
    @RequestMapping(value = "/findDataByMonth",method = RequestMethod.POST)
    public Map<String,Object> mapPercentMonth(@RequestBody Content content){
        Map<String,Object> map = contentService.mapMonthPercent(content);
        return map;
    }
}

